package 算法回顾;

/**
 * @description:
 * @author: 小白白
 * @create: 2021-09-11
 **/

public class JZ04二维数组中的查找 {

    public boolean findNumberIn2DArray(int[][] matrix, int target) {

        int allLength = matrix.length;
        if (allLength == 0) {
            return false;
        }
        int itemLength = matrix[0].length;
        if (itemLength == 0) {
            return false;
        }

        // 右上角开始的二叉树
        int x = 0;
        int y = itemLength - 1;

        while (true) {

            int num = matrix[x][y];
            if (num == target) {
                return true;
            }
            if (num > target) {
                // 当前数字大了,往左边走
                y--;
            } else {
                // 当前数字小了,往下面走
                x++;
            }
            if (x >= allLength || y < 0) {
                break;
            }

        }

        return false;
    }

}
